﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.Service;
using MeTalker.Securitys.Cryptography;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmReg : MeForm
    {
        AESHelper aes = new AESHelper();
        LoginProcHandler login;
        internal frmReg()
        {
            InitializeComponent();
            login = new LoginProcHandler(Login);
        }

        internal frmReg(string url)
            : this()
        {
            //webBrowser1.ScriptErrorsSuppressed = false;
            //webBrowser1.IsWebBrowserContextMenuEnabled = false;
            //webBrowser1.WebBrowserShortcutsEnabled = false;
            ////this.webBrowser1.Url = new Uri(url, UriKind.RelativeOrAbsolute);
            ////this.webBrowser1.DocumentText = "<html><script>window.open('" + url + "');</script></html>";// new Uri(url, UriKind.RelativeOrAbsolute);
            ////this.webBrowser1.DocumentText = "<html><script>window.location.href = '" + url + "';</script></html>";
            //this.webBrowser1.Navigate(url);
        }

        void Login(object sender, byte[] b1)
        {
            HttpClient client = new HttpClient();
            try
            {

                byte[] b = client.Request(string.Format(UtilHelper.USER_REG_URL, UtilHelper.TYPE_USER_REG, new Random().Next()), aes.Encrypt(ToBytes()));
                DispProc(client, b);
            }
            catch
            {
            }
        }

        void DispProc(object sender, byte[] b)
        {
            LoginProcHandler del = delegate(object obj, byte[] b1)
            {
                try
                {
                    if (b == null)
                    {
                        MeMessageBox.ShowInformation("连接服务器失败,请重试!");
                        //MeMessageBox.ShowInformation(DataFactory.Tips.tip_conntect_error_str);
                        return;
                    }

                    string msg = Encoding.UTF8.GetString(b);
                    string[] msgs = msg.Split(',');

                    if (msgs[0].Equals("1"))
                    {
                        MeMessageBox.ShowInformation("恭喜,注册成功,请牢记您的用户名和密码!");
                       // MeMessageBox.ShowInformation(DataFactory.Tips.tip_reg_success_str);
                        Close();
                        return;
                    }
                    else if (msgs[0].Equals("-107")) { //MeMessageBox.ShowInformation(DataFactory.Tips.tip_reg_exist_st); 
                        MeMessageBox.ShowInformation("此用户名已注册,请换一个用户名注册!"); 
                    }
                        //"您在这台机器上已经注册过用户 " + msgs[1].Split('=')[1] + " 了，不能再注册! ");
                    else if (msgs[0].Equals("-106")) { 
                    //    MeMessageBox.ShowInformation(string.Format(DataFactory.Tips.tip_reg_already_fail_st, msgs[1].Split('=')[1]));
                        MeMessageBox.ShowInformation(string.Format("您在这台机器上已经注册过用户 {0} 了,不能再注册!", msgs[1].Split('=')[1]));
                    }
                    else if (msgs[0].Equals("-1"))
                    {
                        //    MeMessageBox.ShowInformation(DataFactory.Tips.tip_reg_fail_st); 
                        MeMessageBox.ShowInformation("注册失败!");
                    }
                    else {
                        MeMessageBox.ShowInformation("注册失败!");
                    }
                }
                catch
                {
                }
                finally
                {
                    btnReg.Enabled = true;
                }
            };
            this.BeginInvoke(del, new object[] { sender, b });
        }

        private void btnReg_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserId.Text))
            {
                MeMessageBox.ShowInformation("用户名不能为空!");
               // MeMessageBox.ShowInformation(DataFactory.Tips.tip_user_not_null_str);
                return;
            }
            if (string.IsNullOrEmpty(txtPass1.Text))
            {
                //MeMessageBox.ShowInformation(DataFactory.Tips.tip_password_not_null_str);
                MeMessageBox.ShowInformation("密码不能为空!");
                return;
            }
            if (!txtPass1.Text.Equals(txtPass2.Text))
            {
                //MeMessageBox.ShowInformation(DataFactory.Tips.tip_pass_consistent_str);
                MeMessageBox.ShowInformation("两次输入的密码不一致,请重新输入!");
                return;
            }
            if (string.IsNullOrEmpty(txtQQ.Text))
            {
                MeMessageBox.ShowInformation("QQ号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(txtEmail.Text))
            {
                MeMessageBox.ShowInformation("邮箱不能为空!");
                return;
            }
            btnReg.Enabled = false;
            login.BeginInvoke(this, null, null, null);
        }

        byte[] ToBytes()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(UtilHelper.CONST_FLAG);
            sb.Append("<root>");
            sb.Append(string.Format("<Email>{0}</Email>", txtEmail.Text));
            sb.Append(string.Format("<Pass>{0}</Pass>", txtPass1.Text));
            sb.Append(string.Format("<QQ>{0}</QQ>", txtQQ.Text));
            sb.Append(string.Format("<Tel>{0}</Tel>", txtTel.Text));
            sb.Append(string.Format("<Uid>{0}</Uid>", txtUserId.Text));
            sb.Append(string.Format("<HD>{0}</HD>", Convert.ToBase64String(Encoding.UTF8.GetBytes(GetHDID()))));
            sb.Append(string.Format("<NI>{0}</NI>", GetNetworkInterfaceToString()));
            sb.Append(string.Format("<CP>{0}</CP>", GetCPUSerialNo()));
            sb.Append("</root>");
            return Encoding.UTF8.GetBytes(sb.ToString());
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}